package com.tqjc.system.system.asyn;

import com.alibaba.fastjson.JSONObject;
import com.tqjc.system.common.constant.AddTaskConstant;
import com.tqjc.system.common.constant.GlobalConstant;
import com.tqjc.system.common.constant.RemoteURLConstant;
import com.tqjc.system.common.entity.BO.AreaScanStatusBO;
import com.tqjc.system.common.entity.BO.PLCWriteRequestParamBO;
import com.tqjc.system.common.enums.AlarmStatusEnum;
import com.tqjc.system.common.enums.ScannerElePowerPLCInfoEnum;
import com.tqjc.system.common.enums.ScannerInfoEnum;
import com.tqjc.system.core.redis.RedisApi;
import com.tqjc.system.core.util.PLCHttpsUtils;
import com.tqjc.system.core.util.SpringUtils;
import com.tqjc.system.system.entity.SysAlarmInfoEntity;
import com.tqjc.system.system.service.SysAlarmInfoService;
import lombok.Builder;
import lombok.extern.slf4j.Slf4j;

/**
 * @description:
 * @Author: 86195
 * @Date: 2024/2/28 14:57
 * @param:
 * @return:
 **/
@Builder
@Slf4j
public class AsynRestartAreaScannerThread extends Thread{

    private SysAlarmInfoEntity alarmInfo;

    @Override
    public void run() {
        //1.任务状态翻转至处理中
        log.info("===========库区扫描仪电源重启线程开始==================");
        SysAlarmInfoService service = SpringUtils.getBean(SysAlarmInfoService.class);
        SysAlarmInfoEntity updateEntity = SysAlarmInfoEntity.builder().idKey(alarmInfo.getIdKey()).status(AlarmStatusEnum.ALARM_STATUS_DOING.getStatus()).build();
        service.updateById(updateEntity);

        //2.扫描仪先断电，再通电  两者间隔3秒
        ScannerElePowerPLCInfoEnum powerPLCInfoEnum = ScannerElePowerPLCInfoEnum.getEnumByScannerKey(alarmInfo.getCarName());
        Object[] plcInfoArray = powerPLCInfoEnum.getPlcInfoArray();
        PLCWriteRequestParamBO writeRequestParamBO = PLCWriteRequestParamBO.builder().address((String) plcInfoArray[0])
                .dataType((Integer) plcInfoArray[1]).macSN((String) plcInfoArray[2]).label((String) plcInfoArray[3]).build();
        writeRequestParamBO.setValue(GlobalConstant.AREA_SCANNER_OUTAGE);
        try {
            PLCHttpsUtils.write(RemoteURLConstant.URL_WRITEBC,writeRequestParamBO);
            sleep(1000 * 5);
            writeRequestParamBO.setValue(GlobalConstant.AREA_SCANNER_ENERGIZE);
            PLCHttpsUtils.write(RemoteURLConstant.URL_WRITEBC,writeRequestParamBO);
            //3.3分钟内，循环读取当前扫描仪状态
            RedisApi redisApi = SpringUtils.getBean(RedisApi.class);
            int i = 0;
            while (true){
                String tag = ScannerInfoEnum.getRedisTag(alarmInfo.getCarName());
                String redisTag = new StringBuilder(GlobalConstant.AREA_SCANNER_REDIS_STATUS_PREFIX).append(tag).toString();
                JSONObject jsonObject = (JSONObject) redisApi.get(redisTag);
                if(jsonObject != null){
                    AreaScanStatusBO areaScanStatusBO = jsonObject.toJavaObject(AreaScanStatusBO.class);
                    String scanStat = areaScanStatusBO.getScanStat();
                    Boolean neededRebootScan = areaScanStatusBO.getNeededRebootScan();
                    if(GlobalConstant.AREA_SCANNER_NORMAL_STATUS.equals(scanStat) && !neededRebootScan){
                        log.info("{}:扫描仪状态已恢复正常",alarmInfo.getCarName());
                        break;
                    }
                }
                if(i > 18){
                    throw new Exception("断电重启扫描仪，三分钟内，扫描仪状态没有恢复");
                }
                Thread.sleep(1000*30);
                i++;
            }

            //4.当前扫描仪状态完成后，翻转对应任务状态为成功.否则3分钟之后，任务状态翻转至失败
            updateEntity.setStatus(AlarmStatusEnum.ALARM_STATUS_DONE.getStatus());
            service.updateById(updateEntity);
            log.info("===========库区扫描仪电源重启线程完成==================");

        } catch (Exception e){
            log.error("异步处理扫描仪重启电源线程异常",e);
            updateEntity.setStatus(AlarmStatusEnum.ALARM_STATUS_ERROR.getStatus());
            service.updateById(updateEntity);
        }



    }
}
